<!--
 * @Author: ChunLai
 * @Date: 2022-03-29 11:10:28
 * @LastEditTime: 2025-11-20 09:45:15
 * @Description: 预试算--年龄选择
 * @FilePath: \01.latest-short-insure\src\views\index\components\precount\SelectAge.vue
-->
<template>
  <div class="pre_count-select-age">
    <div class="select-age-box" @click="timeShow = true">
      <span class="age-box" v-if="birthDate">
        {{ birthDate }}
        {{ birthDate === "" ? "" : `( ${age}周岁 )` }}
      </span>
      <span style="color: #999" v-else>请选择出生日期</span>
      <van-icon name="arrow" class="rotate" />
    </div>

    <!-- 时间选择器弹窗 -->
    <van-popup
      v-model="timeShow"
      overlay-class="lay1_shark"
      position="bottom"
      :style="{ height: '40%' }"
    >
      <van-datetime-picker
        v-model="currentDate"
        type="date"
        :max-date="maxDate"
        :min-date="minDate"
        @cancel="timeShow = false"
        @confirm="handleConfirm"
      />
    </van-popup>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { dateToString } from "@/methods/date.js";
export default {
  name: "SelectAge",
  props: {
    age: {
      type: [Number, String],
      default: () => 18,
    },
    birthDate: {
      type: String,
      default: () => "",
    },
    minDate: {
      type: Date,
      require: true,
    },
    maxDate: {
      type: Date,
      require: true,
    },
    objType: {
      type: String,
      default: () => "bb",
    },
  },
  data() {
    return {
      timeShow: false,
      currentDate: new Date(),
    };
  },
  computed: {
    ...mapGetters({
      insured: "insert/assured",
      mainInsu_e: "risks/mainInsu",
    }),
    hasSexCheckAge() {
      let res = null;
      const mainObj = this.mainInsu_e[0],
        ruleCfg = mainObj.rules || [],
        ruleObj = ruleCfg.find((ele) => ele.type == "sexLimitMaxAge");
      if (ruleObj) {
        res = ruleObj;
      }
      return res;
    },
  },
  methods: {
    handleConfirm(value) {
      let obj = {
        birthDate: dateToString(value),
        age: this.$GlobalFuns.userAge(dateToString(value)),
      };
      // 主险性别限制年龄校验规则(长城/00369)
      if (this.objType == "bb" && this.hasSexCheckAge) {
        const usex = this.insured.sex,
          cfgObj = this.hasSexCheckAge.config.find((ele) => ele.sex == usex);
        if (cfgObj && obj.age > cfgObj.max) {
          let err = `被保险人为${usex == "1" ? "女性" : "男性"}最大投保年龄为${
            cfgObj.max
          }岁`;
          this.$toast(err);
          return;
        }
      }
      this.timeShow = false;
      this.$emit("showTimes", obj, this.objType);
    },
  },
};
</script>

<style lang="less" scoped>
.select-age-box {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  height: 30px;
  line-height: 30px;
  font-size: 15px;

  .age-box {
    color: var(--black-3);
  }
}

.rotate {
  margin-left: 5px;
}
</style>
